﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using CDB.LinqModel.Data;
using CDB.LinqModel;
using CDB.LinqData;


    public partial class back_AddContent : System.Web.UI.Page
    {
        #region Variable Declaration
        IDataRepository _dataObject = new DataRepository();
        Content objContent;
        #endregion //Variable Declaration
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    if (Request.QueryString["Id"] != null)                    
                        bindContent(Convert.ToString(Request.QueryString["Id"]));                    
                }
                else
                    Response.Redirect("login.aspx");
            }
        }

        private void bindContent(string ContentId)
        {
            objContent = new Content(_dataObject);
            objContent.Id = Convert.ToInt16(ContentId);
            objContent.getContentById();

            if (objContent.Id != 0)
            {
                txtPageTitle.Text = objContent.PageTitle;
                txtPageName.Enabled = false;
                txtPageName.Text = objContent.PageName;
                txtKeywords.Text = objContent.MetaKeyword;
                txtDescription.Text = objContent.MetaDescription;
                txtPageContent.InnerHtml = objContent.PageContent;
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            objContent = new Content(_dataObject);

            if (Request.QueryString["Id"] != null)
            {
                objContent.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                objContent.PageTitle = Convert.ToString(txtPageTitle.Text.Trim());
                objContent.PageName = Convert.ToString(txtPageName.Text.Trim());
                objContent.MetaKeyword = Convert.ToString(txtKeywords.Text.Trim());
                objContent.MetaDescription = Convert.ToString(txtDescription.Text.Trim());
                objContent.PageContent = Convert.ToString(txtPageContent.InnerHtml.Trim());
                objContent.UpdateContent();
                
                divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Page updated successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            else
            {
                objContent.PageName = Convert.ToString(txtPageName.Text.Trim());
                objContent.getContentByPageName();

                if (objContent.Id != 0)
                {
                    divMessage.InnerHtml = "<strong>Oh snap!</strong>&nbsp;Page with the same name exists!";
                    divMessage.Attributes.Add("class", "alert alert-error");
                }
                else
                {
                    objContent.PageTitle =Convert.ToString(txtPageTitle.Text.Trim());
                    objContent.PageName =Convert.ToString(txtPageName.Text.Trim());
                    objContent.MetaKeyword =Convert.ToString(txtKeywords.Text.Trim());
                    objContent.MetaDescription =Convert.ToString(txtDescription.Text.Trim());
                    objContent.PageContent = Convert.ToString(txtPageContent.InnerHtml.Trim());
                    objContent.CreateContent();
                    clearControls();
                    divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Page added successfully!";
                    divMessage.Attributes.Add("class", "alert alert-success");
                }
            }
        }
        private void clearControls()
        {
            txtPageTitle.Text = string.Empty;
            txtPageName.Text = string.Empty;
            txtPageContent.InnerText = string.Empty;
            txtKeywords.Text = string.Empty;
            txtDescription.Text = string.Empty;
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Dashboard.aspx");
        }
}
